<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools>
        <template slot="after">
          <el-button
            type="primary"
            size="small"
            @click="addCompetence(1, '0')"
          >添加权限</el-button>
        </template>
      </page-tools>
      <el-table v-loading="loading" border :data="list" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              v-if="row.type === 1"
              type="text"
              @click="addCompetence(2, row.id)"
            >添加</el-button>
            <el-button
              type="text"
              @click="editCompetence(row.id)"
            >编辑</el-button>
            <el-button
              type="text"
              @click="btnCompetence(row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :title="`${titlename}权限点`"
      :visible.sync="dialogVisible"
      @close="btnCancel"
    >
      <!-- // 表单 -->
      <el-form ref="form" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="权限名称" prop="name">
          <el-input
            v-model="formData.name"
            placeholder="请输入权限名称"
            style="width: 80%"
          />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input
            v-model="formData.code"
            placeholder="请输入权限标识"
            style="width: 80%"
          />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input
            v-model="formData.description"
            placeholder="请输入权限描述"
            style="width: 80%"
          />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
            prop="delivery"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center" class="dialog-footer">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取 消</el-button>
          <el-button
            type="primary"
            size="small"
            @click="btnOk"
          >确 定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPermissionList,
  addPermission,
  detailPermission,
  changePermission,
  deletePermission
} from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
export default {
  data() {
    return {
      loading: false,
      list: [], // 页面所有权限
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      },
      dialogVisible: false, // 弹层开关
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' }
        ],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    // 直接当做普通属性调用不加括号
    // 任何data中数据变化立即重新计算
    // 计算属性会缓存
    titlename() {
      return this.formData.id ? '编辑' : '添加'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 点击删除按钮
    async btnCompetence(id) {
      try {
        await this.$confirm('确定要删除该权限吗', '提示', {
          type: 'warning'
        })
        await deletePermission(id)
        await this.getPermissionList()
        this.$message.success('删除权限成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 点击编辑按钮
    async editCompetence(id) {
      this.formData = await detailPermission(id)
      this.dialogVisible = true
      console.log(this.formData)
    },
    // 点击确定按钮
    async btnOk() {
      // 如果有id则为编辑弹层
      await this.$refs.form.validate()
      if (this.formData.id) {
        await changePermission(this.formData)
        this.$message.success('修改权限点成功')
      } else {
        await addPermission(this.formData)
        this.$message.success('添加权限成功')
      }
      this.getPermissionList()
      this.dialogVisible = false
    },
    // 点击取消按钮
    btnCancel() {
      setTimeout(() => {
        this.$refs.form.resetFields()
        this.formData = {}
      }, 500)
      this.dialogVisible = false
    },
    // 点击添加权限点传入type与pid(子节点pid=父节点id)
    async addCompetence(type, id) {
      this.dialogVisible = true
      this.formData.type = type
      this.formData.pid = id
      console.log(this.formData)
    },
    // 获取所有权限
    async getPermissionList() {
      this.loading = true
      this.list = tranListToTreeData(await getPermissionList(), '0')
      console.log(this.list)
      this.loading = false
    }
  }
}
</script>

<style></style>
